/**
 * 该场景固定长宽: 1024 x 768
 * 
 * HTML 设置方案
 * #main (1024 x 768)
 */

import Handler from '../comps/handler';

function createScene(context) {
	let t = new App(context);
	let scene = new context.Scene({
		require : ['pixi'],
		init : (libs) => t.init(libs),
		destroy : () => t.destroy(),
		collectInfo : () => t.collectInfo(),
		onMessage : (key, data) => t.onMessage(key, data),
	});
	t.sendStatus = scene.sendStatus;
	return scene;
}

class App {
	constructor (context) {
		this.context = context;

		/** @type {Handler} */
		this.handler = null;
	}

	init (libs) {
		const initParam = this.context.parseInitParam();
		const params = {
			min : parseInt(initParam.min == null ? 0 : initParam.min),
			max : parseInt(initParam.max == null ? 120 : initParam.max)
		};

		this.handler = new Handler({
			params,
			pixi : libs.pixi,
			// 调用方法: assetPath('img/1.jpg')
			assetPath : this.context.assetPath,
			sendStatus : () => this.sendStatus(),
		});
	}

	destroy () {
		//
	}
	
	collectInfo () {
		if (this.handler) {
			return this.handler.getState();
		}
		return null;
	}

	onMessage (key, data) {
		if (!this.handler) {
			return;
		}

		switch (key) {
			case 'handlerClick': {
				this.handler.machineBegin();
			} break;

			case 'buttonClick': {
				// data : {index}
				let index = data.index;
				this.handler.machineEnd(index);
			} break;

			case 'trackerEnable': {
				// 是否放音频,
				// data : {enable}
				let enable = data.enable;
				this.handler.trackerEnable(enable);
			} break;

			case 'weightChange': {
				// 改变每个值的权重、值的范围
				// data : { candidates }
				let { candidates } = data;
				this.handler.weightChange(candidates);
			} break;

			case 'autoRemove': {
				// 是否启用：已经摇到的数值不再选择
				// data : { enable }
				let { enable } = data;
				this.handler.setAutoRemoveEnable(enable);
			} break;
		}
	}

	sendStatus () {
		// nothing, 等待被覆盖
	}
}

export default createScene;